package ucsyslogic

import (
	"context"
	"fmt"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/usercenter"
	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserAccountLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserAccountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserAccountLogic {
	return &GetUserAccountLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 新增通知消息
func (l *GetUserAccountLogic) GetUserAccount(in *pb.QueryUserAccountReq) (*pb.QueryUserAccountResp, error) {
	defer utils.RecoverPanic()

	userAcc, err := l.svcCtx.UserAccountModel.FindOne(l.ctx, in.Uid)
	if err != nil && err != usercenter.ErrNotFound {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, fmt.Sprintf("err:%+v", err))
	}
	resp := &pb.QueryUserAccountResp{
		Uid:          userAcc.Uid,
		AuthKey:      userAcc.AuthKey,
		CreateTime:   userAcc.CreateTime.Format(dbkey.DateTimeCNFormat),
		UpdateTime:   userAcc.UpdateTime.Format(dbkey.DateTimeCNFormat),
		AuthType:     userAcc.AuthType,
		AccountState: userAcc.AccountState,
		UserType:     userAcc.UserType,
		UserChannel:  userAcc.UserChannel,
		RegIp:        userAcc.RegIp,
		RegDevice:    userAcc.RegDeviceId,
		RegDeviceId:  userAcc.RegDeviceId,
		AppsFlyerUid: userAcc.AppsFlyerUid,
	}
	return resp, nil
}
